package com.qf.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.util.Date;

public class Date2LongSerializer extends JsonSerializer<Date> {

    @Override
    public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        //1. 获取时间的毫秒值.
        long time = date.getTime();
        //2. 毫秒值/1000 = 秒值
        time = time / 1000;
        //3. 写回
        jsonGenerator.writeNumber(time);
    }
}
